﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tessnet2;
using System.Threading;
using System.Reflection;
using System.Drawing;
using System.IO;

namespace OCR
{
    /// <summary>
    /// TESSERACT AÇIK KAYNAK OCR KÜTÜPHANESİNİ KULLANIR.
    /// BU KÜTÜPHANEYİ .NET E UYARLAYAN TESSNET KÜTÜPHANESİNİ KULLANIR
    /// ASSEMBLY VER : 2.32
    /// VARSAYILAN ÇEVİRİ DİLİ : ENG
    /// ÇEVİRİDEN DÖNDÜRÜLEN SONUÇLARDA X < 160 UYGUN BİR ÇEVİRİDİR
    /// UYGULAMANIN BULUNDUĞU DİZİNDE TESSDATA SİMLİ BİR DİZİN OLMAK ZORUNDADIR
    /// BU DİZİNDE ÇEVİRİ İÇİN KULLANILAN DATALAR VARDIR
    /// 
    /// Örnek Kullanım:
    /// OcrReader reader = new OcrReader();
    /// List<Word> res = reader.Read(new Bitmap(pictureBox1.Image));
    /// reader_Finished(res);
    /// </summary>
    public class OcrReader
    {
        private Tesseract ocr;
        private string _lang = "eng";
        private string _tessData = new FileInfo(Assembly.GetExecutingAssembly().FullName).Directory.FullName + "\\OCR\\tessdata";
        protected const bool _digitOnly = false;

        public OcrReader()
        {
            ocr = new Tesseract();
            ocr.Init(_tessData, _lang, false);
            if (_digitOnly)
                ocr.SetVariable("tessedit_char_whitelist", "0123456789");
        }

        public List<Word> Read(Bitmap image)
        {
            try
            {
                List<Word> result = ocr.DoOCR(image, Rectangle.Empty);
                return result;
            }
            catch
            {
                return null;
            }
        }
    }
}
